package user

import (
	"gitee.com/zhanglibin/gmall/model/user"
	// "gitee.com/zhanglibin/gmall/model"
	"gitee.com/zhanglibin/gmall/controller/params"

	"context"
	"errors"
	"fmt"
)

type User struct {
	RegParam   params.RegisterReqParam
	LoginParam *params.LoginReqParam
	// UserInfo   user.User
	GetUserInfoParam params.GetUserInfoParam
	GetUserListParam params.GetUserListParam
	M                user.User
}

func (u *User) Register(ctx context.Context) error {
	var userInfo user.User
	userInfo.Password = u.RegParam.Password
	userInfo.Username = u.RegParam.Username

	err := user.Register(ctx, &userInfo)
	if err != nil {
		fmt.Println("ctx", ctx)
		return errors.New("注册失败")
	}

	return nil
}

func (u *User) Login(ctx context.Context) error {
	var userInfo user.User
	userInfo.Password = u.LoginParam.Password
	userInfo.Username = u.LoginParam.Username

	err := user.Login(ctx, &userInfo)
	if err != nil {
		return err
	}

	return nil
}

func (u *User) GetUserOneInfo(ctx context.Context) (*user.User, error) {
	var userInfo user.User
	userInfo.Id = u.GetUserInfoParam.Id
	userInfo.Username = u.GetUserInfoParam.Username

	if u.GetUserInfoParam.Id == 0 && u.GetUserInfoParam.Username == "" {
		return nil, errors.New("请求参数为空")
	}

	userInfo, err := user.GetUserOneInfo(ctx, &userInfo)
	if err != nil {
		return nil, err
	}

	return &userInfo, err
}

func (u *User) GetUserList(ctx context.Context) (list []user.User, count int64) {
	list, count = user.GetUserList(ctx, u.GetUserListParam.Page, u.GetUserListParam.PageSize)
	return list, count
}

func (u *User) Save(ctx context.Context) error {
	err := user.Update(ctx, &u.M)
	return err
}
